package control;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.KimchiService;
import model.emp.EmpService;
import model.emp.EmpVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class EmpController extends MultiActionController{
	private EmpService empService;
	public void setEmpService(EmpService empService){
		this.empService=empService;
	}
	
	public ModelAndView login(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		  int no=Integer.parseInt(request.getParameter("no"));
		  String password=request.getParameter("pass");
		  EmpVO evo=new EmpVO();
		   evo.setEmp_no(no);
		   evo.setPassword(password);
		  EmpVO rvo=empService.login(evo);
		  HttpSession session=request.getSession();
		  session.setAttribute("rvo", rvo);
		  return new ModelAndView("JsonView","rvo",rvo);
		 }
	

	public ModelAndView noManagement(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		
		ArrayList list=(ArrayList) empService.getAllEmpList();
		System.out.println("리스트="+list);
		int nextNo=empService.nextNo();
		request.setAttribute("nextNo", nextNo);
		HashMap map=new HashMap();
		map.put("nextNo", nextNo);
		map.put("noList", list);
		return new ModelAndView("JsonView","map",map);
	}
	
	public ModelAndView addEmp(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String name=request.getParameter("emp_name");
		String pass=request.getParameter("password");
		String grade=request.getParameter("grade");
		EmpVO evo=new EmpVO();
		evo.setEmp_name(name);
		evo.setPassword(pass);
		evo.setGrade(grade);
		
		System.out.println(evo);
		empService.addEmp(evo);
		return new ModelAndView("JsonView","evo",evo);
	}
	public ModelAndView delEmp(HttpServletRequest request, HttpServletResponse response) throws SQLException{
		String arr=request.getParameter("arr");
		System.out.println("넘어왔지렁"+arr);
		
		if(arr.contains(",")){
			System.out.println("안에 컴마가 있다.");
			String[] delNo= arr.split(",");
//		System.out.println("delNo0="+delNo[0]+" 또 "+delNo[1]);
		
			for (int j = 0; j < delNo.length; j++) {
				empService.delEmp(delNo[j]);
			}
		}
		else{
			System.out.println("안에 컴마가 없다");
			empService.delEmp(arr);
		}
		return new ModelAndView("JsonView");
	}
}
